from django.contrib.auth.models import User

from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from .models import *

class UserSerializer(serializers.ModelSerializer):
    username = serializers.CharField(
        validators = [UniqueValidator(queryset=User.objects.all())]
    )

    def create(self, validated_data):
        password = validated_data.pop('password', None)
        instance = self.Meta.model(**validated_data)
        if password is not None:
            instance.set_password(password)
        instance.save()

        return instance

    def update(self, instance, validated_data):
        for attr, value in validated_data.items():
            if attr == 'password':
                instance.set_password(value)
            else:
                setattr(instance, attr, value)
        instance.save()

        return instance

    class Meta:
        model = User
        fields = ( 'id', 'username', 'password', 'is_superuser')
